<template>
  <Form ref="loginForm"
        :model="form"
        :rules="rules"
        @keydown.enter.native="handleSubmit">
    <FormItem prop="username">
      <Input v-model="form.username"
             placeholder="请输入用户名">
      <span slot="prepend">
        <Icon :size="16"
              type="ios-person"></Icon>
      </span>
      </Input>
    </FormItem>
    <FormItem prop="password">
      <Input type="password"
             v-model="form.password"
             placeholder="请输入密码">
      <span slot="prepend">
        <Icon :size="14"
              type="md-lock"></Icon>
      </span>
      </Input>
    </FormItem>
    <FormItem prop="code">
      <Input v-model="form.code"
             placeholder="请输入验证码"
             type="text">
      <span slot="prepend">
        <Icon :size="14"
              type="md-image"></Icon>
      </span>
      <span slot="append"
            class="svg-wrapper"
            v-html="svg"
            @click="_getCode"></span>
      </Input>
    </FormItem>
    <FormItem>
      <Button @click="handleSubmit"
              type="primary"
              long>登录</Button>
    </FormItem>
  </Form>
</template>
<script>
import { v4 as uuid } from 'uuid'
export default {
  name: 'LoginForm',
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ]
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    },
    codeRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          {
            type: 'string',
            len: 4,
            message: '验证码长度不正确',
            trigger: 'change'
          }
        ]
      }
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        username: 'super_admin',
        password: '',
        code: '',
        sid: ''
      },
      svg: ''
    }
  },
  computed: {
    rules () {
      return {
        username: this.userNameRules,
        password: this.passwordRules,
        code: this.codeRules
      }
    }
  },
  mounted () {
    let sid = ''
    if (localStorage.getItem('sid')) {
      sid = localStorage.getItem('sid')
    } else {
      sid = uuid()
      localStorage.setItem('sid', sid)
    }
    this.$store.commit('setSid', sid)
    this.form.sid = sid
    this._getCode()
  },
  methods: {
    handleSubmit () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit('on-success-valid', {
            username: this.form.username,
            password: this.form.password,
            sid: this.form.sid
          })
        }
      })
    },
    _getCode () {
      Service.getCode({ sid: 'super_admin' }).then(data => {
        this.svg = data.svg
      })
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .ivu-input-group-prepend {
  width: 32px;
}
/deep/ .ivu-input-group-append {
  padding: 0;
}
/deep/ .svg-wrapper {
  display: inline-block;
  padding: 0;
  height: 28px;
  overflow: hidden;
  svg {
    position: relative;
    width: 100px;
    top: -8px;
  }
}
</style>
